package com.victualler.business.inventory.repository.impl;

import com.victualler.business.inventory.dto.print.InventoryPrintDto;
import com.victualler.business.inventory.entity.InventoryOrderDao;
import com.victualler.business.inventory.repository.InventoryOrderQueryRepository;
import com.victualler.core.base.repository.impl.BaseQueryRepositoryImpl;
import com.victualler.core.common.utils.ExceptionWapperUtil;
import com.victualler.core.common.utils.HQLConverterUtil;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Guoshichang
 * @function 封装用于复杂sql查询的API. 为每个实体的repository层提供便利操作
 */
@Component
public class InventoryOrderQueryRepositoryImpl extends BaseQueryRepositoryImpl<InventoryOrderDao,Long> implements InventoryOrderQueryRepository {
    
    /**
     * @author Guoshichang   
     * @since 12:51 on 2018/11/14.
     * @param orderNo orderNo订单号
     * @return List<InventoryPrintDto>
     * @Function  获取库存变更单的实体对象
     */
    public List<InventoryPrintDto> getExportModelList(String orderNo) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT ");
        sql.append("         main.order_no,main.depot_Name,main.storage_Type,main.sum_Quantity, ");
        sql.append("         main.reason,main.made_user, ");
        sql.append("         CONCAT(goods_main. NAME,' [',goods_model_detail. NAME,']') itemName, ");
        sql.append("         item.quantity ");
        sql.append(" FROM ");
        sql.append(" inventory_item item ");
        sql.append(" LEFT JOIN inventory_order main ON item.parent_id = main.id ");
        sql.append(" LEFT JOIN goods_main ON goods_main.id = item.goods_id ");
        sql.append(" LEFT JOIN goods_model_detail ON goods_model_detail.id = item.goods_model_id ");
        sql.append(" AND goods_model_detail.parent_id = item.goods_id ");
        sql.append(" WHERE ");
        sql.append("         order_No = ? ");
        try {
            return HQLConverterUtil.findVOListBySql(sql.toString(),new Object[]{orderNo},InventoryPrintDto.class,this.getEntityManager());
        } catch (Exception e) {
            throw ExceptionWapperUtil.wapper(e);
        }
    }
}
